import random

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.common.proxy import Proxy, ProxyType

from fake_useragent import UserAgent


class Browser:

    """ Класс представляющий браузер """

    def get_browser(self, display_browser, proxy=False, private_proxy=False):

        """ Настроить и получить браузер """

        user_agent = UserAgent()
        user_agent = user_agent.random
        driver = GeckoDriverManager().install()

        options = webdriver.FirefoxOptions()
        options.set_preference('general.useragent.override', user_agent)
        options.set_preference('dom.webdriver.enabled', False)

        if not display_browser == 'yes':  # Если браузер не нужно отображать

            options.headless = True

        if proxy:  # Если собираемся парсить через прокси

           browser =  self.get_browser_with_proxy(driver, user_agent, options, proxy)
           return browser

        elif private_proxy:  # Если нужно парсить через приватные прокси

            pass

        else:  # Если не собираемся парсить через прокси

            browser = webdriver.Firefox(
                driver,
                options=options
            )

            return browser


    def get_browser_with_proxy(self, driver,  user_agent, options, proxy):

        """ Возвращаем браузер с прокси """

        def choose_a_proxy(proxy):

            """ Получаем прокси """

            match proxy:

                case 'random', proxy_list:

                    random_proxy = random.choice(proxy_list)
                    return random_proxy.split(':')

                case _:

                    print(f'Непонятно какой прокси выбирать\nproxy - {proxy}')


        # ВЫШЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

        selected_proxy = choose_a_proxy(proxy)
        print(f'Используем прокси - {selected_proxy}')

#        fp = webdriver.FirefoxProfile()
#        fp.set_preference("network.proxy.type", 1)
#        fp.set_preference("network.proxy.http", selected_proxy[0])
#        fp.set_preference("network.proxy.http_port",int(selected_proxy[1]))
#        fp.set_preference("network.proxy.https", selected_proxy[0])
#        fp.set_preference("network.proxy.https_port",int(selected_proxy[1]))
#        fp.set_preference("network.proxy.ssl", selected_proxy[0])
#        fp.set_preference("network.proxy.ssl_port",int(selected_proxy[1]))
#        fp.set_preference("network.proxy.ftp",selected_proxy[0])
#        fp.set_preference("network.proxy.ftp_port",int(selected_proxy[1]))
#        fp.set_preference("network.proxy.socks", selected_proxy[0])
#        fp.set_preference("network.proxy.socks_port",int(selected_proxy[1]))
#        fp.update_preferences()

#        prox = Proxy()
#        prox.proxy_type = ProxyType.MANUAL
#
#        prox.http_proxy = selected_proxy
#        #prox.socks_proxy = selected_proxy
#        prox.ssl_proxy = selected_proxy
#
#        capabilities = webdriver.DesiredCapabilities.FIREFOX
#        prox.add_to_capabilities(capabilities)

        http_proxy  = f"http://{selected_proxy[0]}:{selected_proxy[1]}"
        https_proxy  = f"https://{selected_proxy[0]}:{selected_proxy[1]}"

        webdriver.DesiredCapabilities.FIREFOX['proxy']={
            "httpProxy":http_proxy,
            "sslProxy":https_proxy,
            "proxyType":"MANUAL"
        }

        browser = webdriver.Firefox(
            driver,
            options=options,
        )

        return browser
